﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE.Tool.UserControls;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_FrequencyEdit : DevExpress.XtraEditors.XtraForm
	{
		public string frequencyId;
		public string frequencyName;
		public string frequencyNameEnglish;
		private ucFrequency parent;

		public frm_FrequencyEdit(ucFrequency frmFrequency)
		{
			InitializeComponent();
			parent = frmFrequency;
			this.CancelButton = this.btCancel;
		}

		private void btSave_Click(object sender, EventArgs e)
		{
			FrequencyHelper helper = new FrequencyHelper(new Frequency());
			if(GetData(helper.ValueObject))
			{
				Utilities.Timer timer = new Utilities.Timer();

				if(helper.Update() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.UpdateSuccess);
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.UpdateFailed);
				}
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void frm_FrequencyEdit_Load(object sender, EventArgs e)
		{
			BindData();
		}

		#region Private methods

		private bool CheckDuplicate(string id)
		{
			FrequencyHelper helper = new FrequencyHelper(new Frequency());
			helper.ValueObject.FrequencyID = id;
			DataTable dt = helper.GetOne();
			if(dt.Rows.Count > 0)
				return true;
			return false;

		}
		private bool GetData(Frequency o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(txtFrequencyID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Frequency.FrequencyIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtFrequencyName.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Frequency.FrequencyNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(string.Compare(txtFrequencyID.Text.Trim(), frequencyId) != 0)
			{
				if(CheckDuplicate(txtFrequencyID.Text.Trim()))
				{
					MessageBox.Show(ToolConstants.Message.Frequency.FrequencyHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
					return false;
				}
			}

			#endregion

			o.TempId = txtFrequencyID.Text;
			o.FrequencyID = frequencyId;
			o.FrequencyName = txtFrequencyName.Text;
			o.FrequencyName_English = txtFrequencyNameEnglish.Text;

			return true;
		}

		private void BindData(Frequency o)
		{
			txtFrequencyID.Text = o.FrequencyID;
			txtFrequencyName.Text = o.FrequencyName;
			txtFrequencyNameEnglish.Text = o.FrequencyName_English;
		}

		private void BindData()
		{
			txtFrequencyID.Text =frequencyId;
			txtFrequencyName.Text = frequencyName;
			txtFrequencyNameEnglish.Text = frequencyNameEnglish;
		}

		#endregion

	}
}